package controller

import (
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"hytx_manager/models"
	"hytx_manager/pkg/setting"
	"strconv"
	"time"
)
func FixedTemplateList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	var res []*models.FixedTemplate
	m := models.DB.Model(&models.FixedTemplate{}).
		Select("`id`,`name`,`cover`,`sort`,`type`,`describe`,`is_enabled`,`created_at`,`updated_at`,`path`,`cate`,`clazz`")
	var count int
	m.Count(&count)
	paginate := models.NewPage(page, limit, count)
	m.Offset(paginate.Offset).Limit(paginate.Limit).Find(&res)
	render(c, gin.H{
		"paginate": paginate,
		"data":     res,
	})
}
func FixedTemplateGet(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	var res models.FixedTemplate

	models.DB.Model(&models.FixedTemplate{}).Where("id=?", id).First(&res)
	render(c, res)
}
/**
	添加系统模板
 */
func AddFixedTemplate(c *gin.Context) {
	var ft models.FixedTemplate
	c.ShouldBind(&ft)
	valid := validation.Validation{}
	b, err := valid.Valid(&ft)
	if err != nil {
		fail(c, "")
		return
	}
	if !b {
		for _, err := range valid.Errors {
			fail(c, err.Key+" "+err.Message)
			return
		}
	}
	ft.CreatedAt = time.Now()
	ft.IsEnabled = 1
	models.DB.Model(&models.FixedTemplate{}).Create(&ft)
	success(c)
}
/**
	编辑系统模板
 */
func EditFixedTemplate(c *gin.Context) {
	var ft models.FixedTemplate
	c.ShouldBind(&ft)
	ft.UpdatedAt = time.Now()
	models.DB.Model(&models.FixedTemplate{}).Where("id=?",ft.ID).Update(ft)
	success(c)
}
/**
	删除系统模板
 */
func DelFixedTemplate(c *gin.Context) {

	id := c.Query("id")

	models.DB.Delete(&models.FixedTemplate{}, "id=?", id)
	success(c)
}

func AbleFixedTemplate(c *gin.Context) {
	id,_ := strconv.Atoi(c.PostForm("id"))
	isEnabled, _ := strconv.Atoi(c.PostForm("is_enabled"))
	models.DB.Model(&models.FixedTemplate{}).Where("id=?", id).Update("is_enabled", isEnabled)
	success(c)
}


/**
	系统模板分类
 */
func FixedTemplateCategoryGet(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	var res models.FixedTemplateCategory

	models.DB.Model(&models.FixedTemplateCategory{}).Where("id=?", id).First(&res)
	render(c, res)
}
func FixedTemplateCategoryList(c *gin.Context) {
	var res []*models.FixedTemplateCategory
	models.DB.Model(&models.FixedTemplateCategory{}).Find(&res)
	render(c, res)
}
func AddFixedTemplateCategory(c *gin.Context) {
	var fc models.FixedTemplateCategory
	c.ShouldBind(&fc)
	fc.CreatedAt = time.Now()
	models.DB.Model(&models.FixedTemplateCategory{}).Create(&fc)
	success(c)
}

func EditFixedTemplateCategory(c *gin.Context) {
	id, _ := strconv.Atoi(c.PostForm("id"))
	var fc models.FixedTemplateCategory
	c.ShouldBind(&fc)
	models.DB.Model(&models.FixedTemplateCategory{}).Where("id=?", id).Update(&fc)
	success(c)
}

func DelFixedTemplateCategory(c *gin.Context) {
	id := c.Query("id")

	models.DB.Delete(&models.FixedTemplateCategory{}, "id=?", id)
	success(c)
}

func AbleFixedTemplateCategory(c *gin.Context) {
	id,_ := strconv.Atoi(c.PostForm("id"))
	isEnabled, _ := strconv.Atoi(c.PostForm("is_enabled"))
	models.DB.Model(&models.FixedTemplateCategory{}).Where("id=?", id).Update("is_enabled", isEnabled)
	success(c)
}
/**
	自定义分类
 */

func CustomCategoryGet(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	var res models.CustomCategory

	models.DB.Model(&models.CustomCategory{}).Where("id=?", id).First(&res)
	render(c, res)
}
func CustomCategoryList(c *gin.Context) {
	var res []*models.CustomCategory
	models.DB.Model(&models.CustomCategory{}).Find(&res)
	render(c, res)
}
func AddCustomCategory(c *gin.Context) {
	var cc models.CustomCategory
	c.ShouldBind(&cc)
	cc.IsEnabled = 1
	cc.CreatedAt = time.Now()
	models.DB.Model(&models.CustomCategory{}).Create(&cc)
	success(c)
}

func EditCustomCategory(c *gin.Context) {
	id, _ := strconv.Atoi(c.PostForm("id"))
	var cc models.CustomCategory
	c.ShouldBind(&cc)
	cc.IsEnabled = 1
	cc.UpdatedAt = time.Now()
	models.DB.Model(&models.CustomCategory{}).Where("id=?", id).Update(&cc)
	success(c)
}

func DelCustomCategory(c *gin.Context) {
	id := c.Query("id")

	models.DB.Delete(&models.CustomCategory{}, "id=?", id)
	success(c)
}

func AbleCustomCategory(c *gin.Context) {
	id,_ := strconv.Atoi(c.PostForm("id"))
	isEnabled, _ := strconv.Atoi(c.PostForm("is_enabled"))
	models.DB.Model(&models.CustomCategory{}).Where("id=?", id).Update("is_enabled", isEnabled)
	success(c)
}
/**
	自定义模板
 */
func CustomComponentGet(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	var res models.CustomComponent

	models.DB.Model(&models.CustomComponent{}).Where("id=?", id).First(&res)
	render(c, res)
}
func CustomComponentList(c *gin.Context) {
	var res []*models.CustomComponent
	m := models.DB.Model(&models.CustomComponent{}).
		Select("`id`,`cate`,`name`,`cover`,`describe`,`has_vip`,`price`,`type`,`ratio`,`is_slide`,`is_enabled`,`created_at`,`updated_at`")
	if c.Query("cate_id") != "" {
		m = m.Where("cate=?", c.Query("cate_id"))
	}
	if c.Query("ratio") != "" {
		m = m.Where("ratio=?", c.Query("ratio"))
	}
	if c.Query("type") != "" {
		m = m.Where("type=?", c.Query("type"))
	}
	if c.Query("is_slide") != "" {
		m = m.Where("is_slide=?", c.Query("is_slide"))
	}
	m.Find(&res)
	render(c, res)
}
func AddCustomComponent(c *gin.Context) {
	var cc models.CustomComponent
	c.ShouldBind(&cc)
	cc.IsEnabled = 1
	cc.CreatedAt = time.Now()
	models.DB.Model(&models.CustomComponent{}).Create(&cc)
	success(c)
}

func EditCustomComponent(c *gin.Context) {
	var cc models.CustomComponent
	c.ShouldBind(&cc)
	cc.UpdatedAt = time.Now()
	id, _ := strconv.Atoi(c.PostForm("id"))
	models.DB.Model(&models.CustomComponent{}).Where("id=?", id).Update(&cc)
	success(c)
}

func DelCustomComponent(c *gin.Context) {
	id := c.Query("id")

	models.DB.Delete(&models.CustomComponent{}, "id=?", id)
	success(c)
}

func AbleCustomComponent(c *gin.Context) {
	id,_ := strconv.Atoi(c.PostForm("id"))
	isEnabled, _ := strconv.Atoi(c.PostForm("is_enabled"))
	models.DB.Model(&models.CustomComponent{}).Where("id=?", id).Update("is_enabled", isEnabled)
	success(c)
}
/**
	自定义模板背景
 */

func CustomBackgroundGet(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	var res models.CustomBackground

	models.DB.Model(&models.CustomBackground{}).Where("id=?", id).First(&res)
	render(c, res)
}

func CustomBackgroundList(c *gin.Context) {
	var res []*models.CustomBackground
	models.DB.Model(&models.CustomBackground{}).Find(&res)
	render(c, res)
}
func AddCustomBackground(c *gin.Context) {
	var cc models.CustomBackground
	c.ShouldBind(&cc)
	cc.IsEnabled = 1
	cc.CreatedAt = time.Now()
	models.DB.Model(&models.CustomBackground{}).Create(&cc)
	success(c)
}

func EditCustomBackground(c *gin.Context) {
	var cc models.CustomBackground
	c.ShouldBind(&cc)
	cc.UpdatedAt = time.Now()
	id, _ := strconv.Atoi(c.PostForm("id"))
	models.DB.Model(&models.CustomBackground{}).Where("id=?", id).Update(&cc)
	success(c)
}

func DelCustomBackground(c *gin.Context) {
	id := c.Query("id")

	models.DB.Delete(&models.CustomBackground{}, "id=?", id)
	success(c)
}

func AbleCustomBackground(c *gin.Context) {
	id,_ := strconv.Atoi(c.PostForm("id"))
	isEnabled, _ := strconv.Atoi(c.PostForm("is_enabled"))
	models.DB.Model(&models.CustomBackground{}).Where("id=?", id).Update("is_enabled", isEnabled)
	success(c)
}